<?php
/**
 * 使用示例
 * @author M2
 * @version 1.0
 * create Date 2013-11-5
 * update Date 2013-11-7
 * type取值：Channels(统计栏目数) Contents(统计内容数) Comments(统计评论数)
 * <yl:count type="Contents"/> 当前栏目的内容数
 * <yl:contents><yl:count type="Comments"></yl:count></yl:contents> 当前内容的评论数
 * 配合其他标签一起使用
 */
class YlCountTaglib {
	public function parse($content,$arr=array()) {
        if (stripos($content, 'yl:count') !== false) {
            $matches = preg_parse($content, 'yl:count');
            foreach ($matches as $match) {
                $type = getParseOneAttr($match, 'type');
                $channelname = getParseOneAttr($match, 'channelname');
                if(!empty($type)) {
                    if($type === 'Contents') {
                        if(!empty($channelname)) {
                            $findCount = M('cms_category')->field('content_num')->where('cname="'.$channelname.'"')->find();
                            $count = $findCount['content_num'];
                        } elseif(!empty($arr)) {
                            $data['id'] = $arr['id'];
                            $findCount = M('cms_category')->field('content_num')->where($data)->find();
                            $count = $findCount['content_num'];
                        }
                    } elseif ($type === 'Comments') {
                        if(!empty($arr)) {
                            $data['article_id'] = $arr['id'];
                            $count = M('cms_comments')->where($data)->count(1);
                        }
                    }
                }
                $content = str_replace($match, $count, $content);
            }
        }
        return $content;
	}
}